<?php

require_once('bootstrap.php');

// read the HTTP Request
$method = $_SERVER['REQUEST_METHOD'];
$uri    = rip_parse_uri();

try {

  rip_setup_db();

  rip_map_db();

  rip_validate_resource($uri);

  switch ($method) {
    case 'PUT':
      $result = rip_create($uri);
      break;
    case 'GET':
      $result = rip_read($uri);
      break;
    case 'POST':
      $result = rip_update($uri);
      break;
    case 'DELETE':
      $result = rip_delete($uri);
      break;
    default:
      watchdog('Wrong request type', WATCHDOG_ERROR);
      exit(WATCHDOG_ERROR);
  }
} catch (Exception $e) {
  watchdog(t('Critical error'), WATCHDOG_CRITICAL);
  exit(WATCHDOG_CRITICAL);
}

print(rip_parse_output($result));